package com.teamone.etl.apimanager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.apimanager.dao.ApiChargeDao;
import com.teamone.etl.apimanager.dao.ApiErrorCodeDao;
import com.teamone.etl.apimanager.dao.ApiSourceDao;
import com.teamone.etl.apimanager.entity.ApiCharge;
import com.teamone.etl.apimanager.entity.ApiErrorCode;
import com.teamone.etl.apimanager.entity.ApiSource;
import com.teamone.etl.apimanager.request.ApiSourceQuery;
import com.teamone.etl.apimanager.service.ApiSourceService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 源接口表(ApiSource)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 18:32:06
 */
@Service("apiSourceService")
@Transactional(rollbackFor = Exception.class)
public class ApiSourceServiceImpl extends ServiceImpl<ApiSourceDao, ApiSource> implements ApiSourceService {

    @Resource
    private ApiErrorCodeDao apiErrorCodeDao;
    @Resource
    private ApiChargeDao apiChargeDao;

    @Override
    public PageInfo<ApiSource> selectAll(ApiSourceQuery apiSourceQuery) {
        PageHelper.startPage(apiSourceQuery.getPageNum(),apiSourceQuery.getPageSize());
        List<ApiSource> apiSources=baseMapper.selectAll(apiSourceQuery);
        List<ApiCharge> apiCharges = apiChargeDao.selectAll();
        apiSources.forEach(apiSource -> apiCharges.forEach(apiCharge -> {
            if(apiSource.getId().equals(apiCharge.getApiSourceId())){
                apiSource.setApiCharge(apiCharge);
            }
        }));
        return new PageInfo<>(apiSources);
    }

    @Override
    public void saveOne(ApiSource apiSource) {
        baseMapper.saveOne(apiSource);
    }

    @Override
    public void logicDeleteById(Integer id) {
        baseMapper.logicDeleteById(id);
    }

    @Override
    public void updateOne(ApiSource apiSource) {
        baseMapper.updateOne(apiSource);
    }

    @Override
    public List<ApiErrorCode> selectErrorCodeByApiSourceId(Integer apiSourceId) {
        return apiErrorCodeDao.selectErrorCodeByApiSourceId(apiSourceId);
    }

    @Override
    public ApiSource selectByApiSourceId(Integer apiSourceId) {
        ApiSource apiSource=baseMapper.selectByApiSourceId(apiSourceId);
        ApiCharge apiCharge=apiChargeDao.selectByApiSourceId(apiSourceId);
        if(apiCharge!=null){
            apiSource.setApiCharge(apiCharge);
        }
        return apiSource;
    }
}